<?php

namespace logInModel;
require_once 'model/GetMembers.php';

class LogInModel{
	
	private $getMembers;
	private $LoggedInStatus;
	private static $loggedInSession = "loggedInSession";
	private static $UsernameSession = "UsernameSession";
	private $message;
	
	public function __construct(){
		$this->getMembers = new \getMembers\GetMembers();
	}
	
	//@return string (message)
	public function getMessage(){
		if(isset($this->message)){
			return $this->message;
		}
	}
	
	//sends user and password to addUser() in model
	public function addUser($user, $password){
		if($this->checkAddUser($user, $password)){
			$this->getMembers->addUser($user, $password);
			$this->message = 2;
		}else{echo"bajs";}
	}
	
	//Validate when you try to add a user
	private function checkAddUser($user, $password){
		if($user == null || $user == "" || $password == null || $password == ""){
			$this->message = 8;
			return FALSE;
		}else if(count($user) > 5 || count($password) > 5){
			$this->message = 9;
			return FALSE;
		}else{
			return TRUE;
		}
	}
	
	//@return string (username)
	public function getUsername(){
		if(isset($_SESSION[self::$UsernameSession]))
		return $_SESSION[self::$UsernameSession];
	}
	
	//tries to log in
	//@return bool
	public function tryLogIn($username, $password){
		if($this->getMembers->checkMember($username, $password) == TRUE){ 
			$this->createSession();
			$_SESSION[self::$UsernameSession] = $username;
			$this->message = 1;
			return $this->LoggedInStatus = TRUE;
		}else{
			$this->message = 5;
			return $this->LoggedInStatus = FALSE;
		}	
	}
	
     //logs out || destroys sesseions and cookies
    public function logOut(){            
        unset($_SESSION[self::$loggedInSession]);
   		session_destroy();
		$this->message = 6;
    }
	
	 //checks if sesseion logged in is set.
    public function checkIfLoggedIn(){
        try{	
				$this->checkSession();
	            if(isset($_SESSION[self::$loggedInSession])){
	                    return TRUE;
	            }else{
	                    return FALSE;
	            }
            }
        catch(Exception $e){
                echo "Something wrong in model";
        }
    }

    /*
     * create session 
     */
    private function createSession(){
            $_SESSION[self::$loggedInSession] = true; 
            $_SESSION["session_security"] = array();
            $_SESSION["session_security"]["webbläsare"] = $this->getUserAgent();
    }
    
    /*
     * checks session
     */
    private function checkSession(){
            if($_SESSION["session_security"]["webbläsare"] == $this->getUserAgent()){
            }else if(!empty($_SESSION)){
				if(isset($_SESSION[self::$loggedInSession])){
					session_destroy();
                	unset($_SESSION[self::$loggedInSession]);
				}
				$this->message = 7;
            }          
    }
    
    // Magic happens in this function to find out the users browser
    //http://stackoverflow.com/questions/9693574/user-agent-extract-os-and-browser-from-string-php
	private static function getUserAgent(){
    	static $agent = null;

	    if ( empty($agent) ) {
	        $agent = $_SERVER['HTTP_USER_AGENT'];
	
	        if ( stripos($agent, 'Firefox') !== false ) {
	            $agent = 'firefox';
	        } elseif ( stripos($agent, 'MSIE') !== false ) {
	            $agent = 'ie';
	        } elseif ( stripos($agent, 'iPad') !== false ) {
	            $agent = 'ipad';
	        } elseif ( stripos($agent, 'Android') !== false ) {
	            $agent = 'android';
	        } elseif ( stripos($agent, 'Chrome') !== false ) {
	            $agent = 'chrome';
	        } elseif ( stripos($agent, 'Safari') !== false ) {
	            $agent = 'safari';
	        } elseif ( stripos($agent, 'AIR') !== false ) {
	            $agent = 'air';
	        } elseif ( stripos($agent, 'Fluid') !== false ) {
	            $agent = 'fluid';
	        }
	
	    }
	    return $agent;
	}


}
